package com.yoda.service.Impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yoda.constant.MessageConstant;
import com.yoda.dto.CatePageDTO;
import com.yoda.entity.YddCate;
import com.yoda.mapper.YddCateMapper;
import com.yoda.result.PageResult;
import com.yoda.result.Result;
import com.yoda.service.IYddCateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author itcast
 * @since 2023-05-15
 */
@Service
public class YddCateServiceImpl extends ServiceImpl<YddCateMapper, YddCate> implements IYddCateService {

    @Autowired
    private YddCateMapper yddCateMapper;
    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @Override
    public Result getById(String id) {

        YddCate yddCate = yddCateMapper.selectById(id);
        return Result.success(yddCate);
    }

    /**
     * 分类列表-分页
     *
     * @param dto
     * @return
     */
    @Override
    public Result pageCate(CatePageDTO dto) {
        if (dto.getPage()==null){
            dto.setPage(1);
        }
        if (dto.getLimit()==null){
            dto.setLimit(5);
        }
       //创建条件
        LambdaQueryWrapper<YddCate> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        lambdaQueryWrapper.like(dto.getCatename()!=null,YddCate::getCatename,dto.getCatename());
        //        2.创建IPage分页对象,设置分页参数
        IPage<YddCate> page = new Page<YddCate>(dto.getPage(),dto.getLimit());
        IPage<YddCate> iPage = yddCateMapper.selectPage(page, lambdaQueryWrapper);

        PageResult pageResult = new PageResult();
        pageResult.setCurrentPage(iPage.getCurrent());
        pageResult.setPageSize( iPage.getSize());
        pageResult.setTotalPages( iPage.getPages());
        pageResult.setCount( iPage.getTotal());
        pageResult.setData(iPage.getRecords());
        return Result.success(pageResult);
    }

    /**
     * 新增分类
     *
     * @param yddCate
     * @return
     */
    @Override
    public Result add(YddCate yddCate) {

        LambdaQueryWrapper<YddCate> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(yddCate.getCatename()!=null,YddCate::getCatename,yddCate.getCatename());
        List<YddCate> list = list(wrapper);
        if (list!=null&&list.size()>0){
            return Result.error(MessageConstant.UNKNOWN_ERROR);
        }
        yddCate.setDefaultData("1");
        save(yddCate);
        return Result.success();
    }

    /**
     * 修改分类
     *
     * @param yddCate
     * @return
     */
    @Override
    public Result update(YddCate yddCate) {
        LambdaQueryWrapper<YddCate> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(yddCate.getCatename()!=null,YddCate::getCatename,yddCate.getCatename());
        List<YddCate> list = list(wrapper);
        if (list!=null&&list.size()>0){
            return Result.error(MessageConstant.UNKNOWN_ERROR);
        }
        updateById(yddCate);
        return Result.success();
    }

    /**
     * 分类列表
     *
     * @return
     */
    @Override
    public Result selectAll() {
        List<YddCate> list = list();
        return Result.success(list);
    }
}
